<?php
/*
*  -------------------------------------------------
*   @file		: .php
*   @link		:CPC  www.chinapet.com
*   @copyright	: 2002-2012 Xingmo Inc
*   @author		: Laipiyang <462166282@qq.com>
*   @blog_url   : http://www.icuir.com/
*   @blog_url   : http://blog.sina.com.cn/xmphp
*   @date		:
*   @update		:
*  -------------------------------------------------
*/

include_once './include/init.inc.php';
$filename=basename(__FILE__);
//checklogin();
$act=isset ( $_GET['act'] ) ? trim ( $_GET['act'] ) : '' ;
if($act=='add')
{
	 $smarty->assign('sort_list',$db->fetch_all("SELECT * FROM `info_q_usual_sort` WHERE `type`='2' ORDER BY `dateline` DESC")); //传优惠卷下拉
	 $smarty->assign('a',array('title'=>'添加优惠卷','act'=>'insert','btn'=>'添加优惠卷完成'));
	 $smarty->display(ADMIN_TPL.'coupon_info.dwt');

}
else if ($act=='insert')
{
  $coupon_name=isset ( $_POST['coupon_name'] ) ? trim ( $_POST['coupon_name'] ) : '' ;
  $sort_id=isset ( $_POST['sort_id'] ) ? intval ( $_POST['sort_id'] ) : 0 ;
  $closing_date = isset ( $_POST['closing_date'] ) ? trim ( $_POST['closing_date'] ) : '' ;
  $order = (isset ( $_POST['order']) && $_POST['order']!='') ? intval ( $_POST['order'] ) : 10 ;
  $enable=isset ( $_POST['enable'] ) ? trim ( $_POST['enable'] ) :1 ;
  $photo=isset($_FILES['photo']) ? $_FILES['photo'] : array();
  $description =isset ( $_POST['description'] ) ? trim ( $_POST['description'] ) : '' ;
  $dateline=time();
  if(strlen($coupon_name)<4 || strlen($coupon_name) >30)
  {
	 alert('优惠券名称不能小于2个字符或大于30个字符');
	 exit();
  }
  if($db->fetch_one("SELECT * FROM `info_q_coupon` WHERE `coupon_name`='{$coupon_name}'"))
  {
	 alert('优惠卷名称已经存在,请重新输入');
	 exit();
  }
  if($sort_id==0 || empty($sort_id))
  {
	 alert('对不起,必须选择一个分类');
	 exit();
  }

  if(empty($closing_date) || $closing_date=='')
  {
	 alert('对不起,截止日期必选!');
	 exit;
  }
  if($photo['name']=='')
  {
	  alert('必须选择一张图');
	  exit();
  }
  else
  {
	    if(is_uploaded_file($photo['tmp_name']))
	    {
			if($photo['error']=='0')
			{
				$file_dir = ROOT.'upload/coupon/';  //设置上传目录
				$upload = new UploadFile;
				$imgName = $upload->upload($file_dir,$photo,10242880);

			}
			else
			{
				alert('对不起,上传文件失败,请稍后重试...');
				exit();
			}
	    }
  }
   // sql insert
   $sql="INSERT INTO `info_q_coupon` (`coupon_name`,`sort_id`,`closing_date`,`order`,`enable`,`photo`,`description`,`dateline`) VALUES ('{$coupon_name}','{$sort_id}','{$closing_date}','{$order}','{$enable}','{$imgName}','{$description}','{$dateline}')";
    if($db->query($sql))
    {
		 alert('优惠卷添加成功',$filename);
		 exit();
    }
	else
	{
		alert('优惠添加失败');
		exit();
	}

}else if ($act=='edit')
{
	 $coupon_id=isset ( $_GET['coupon_id'] ) ? intval ( $_GET['coupon_id'] ) : 0 ;
	 if($coupon_id<1)
	 {
		alert('操作非法');
		exit();
	 }
	 $sql="SELECT * FROM `info_q_coupon` WHERE `coupon_id`='{$coupon_id}' ";
	 $query=$db->fetch_one($sql);
	 if(!$query)
	 {
		 alert('数据不存在');
		 exit();
	 }
	 echo '<pre>';
	 print_r ($query);
	 echo '</pre>';
	 $smarty->assign('sort_list',$db->fetch_all("SELECT * FROM `info_q_usual_sort` WHERE `type`='2' ORDER BY `dateline` DESC"));
	 $smarty->assign('r',$query);
	 $smarty->assign('a',array('title'=>'修改优惠卷','act'=>'update','btn'=>'修改优惠卷完成'));
	  $smarty->display(ADMIN_TPL.'coupon_info.dwt');


}
else if ($act=='update')
{
	  $coupon_id=isset ( $_POST['coupon_id'] ) ? intval ( $_POST['coupon_id'] ) : 0;
	  $coupon_licuicui=isset ( $_POST['coupon_licuicui'] ) ? trim ( $_POST['coupon_licuicui'] ) : '';
	  $coupon_name=isset ( $_POST['coupon_name'] ) ? trim ( $_POST['coupon_name'] ) : '' ;
	  $sort_id=isset ( $_POST['sort_id'] ) ? intval ( $_POST['sort_id'] ) : 0 ;
	  $closing_date = isset ( $_POST['closing_date'] ) ? trim ( $_POST['closing_date'] ) : '' ;
	  $order = (isset ( $_POST['order']) && $_POST['order']!='') ? intval ( $_POST['order'] ) : 10 ;
	  $enable=isset ( $_POST['enable'] ) ? trim ( $_POST['enable'] ) :1 ;
	  $photo=isset($_FILES['photo']) ? $_FILES['photo'] : array();
	  $description =isset ( $_POST['description'] ) ? trim ( $_POST['description'] ) : '' ;
	  $dateline=time();
	  if($coupon_id<1)
	  {
		 alert('操作非法');
		 exit();
	  }
	  if(strlen($coupon_name)<4 || strlen($coupon_name) >30)
	  {
		 alert('优惠券名称不能小于2个字符或大于30个字符');
		 exit();
	  }
	  if($db->fetch_one("SELECT * FROM `info_q_coupon` WHERE `coupon_name`='{$coupon_name}' AND `coupon_id`<>{$coupon_id}"))
	  {
		 alert('对不起,不能修改成为和该下同名的优惠卷');
		 exit();
	  }
	  if($sort_id==0 || empty($sort_id))
	  {
		 alert('对不起,必须选择一个分类');
		 exit();
	  }
	 if(empty($closing_date) || $closing_date=='')
	 {
		 alert('对不起,截止日期必选!');
		 exit;
	 }
	  if(!empty($photo['name']))
	  {
                 if(is_uploaded_file($photo['tmp_name']))
				 {
					if($photo['error']=='0')
					{
						$file_dir = ROOT.'upload/coupon/';  //设置上传目录
						$upload = new UploadFile;
						$imgName = $upload->upload($file_dir,$photo,10242880);
						//删除老图
//						$path=$file_dir.substr($coupon_licuicui,0,2);
//						if(is_dir($path))
//						{
//							delDirAndFile($path);  //删除目录以及目录下文件
//						}
					}
					else
					{
						alert('对不起,上传文件失败,请稍后重试...');
						exit();
					}
				}
	  }
	  else
	  {
          $imgName=$coupon_licuicui;  //如果为空则使用原图

	  }
	  // sql update
	  $sql="UPDATE `info_q_coupon` SET `coupon_name`='{$coupon_name}',`sort_id`='{$sort_id}',`closing_date`='{$closing_date}',`photo`='{$imgName}',`enable`='{$enable}',`order`='{$order}',`description`='{$description}',`dateline`='{$dateline}'  WHERE `coupon_id`='{$coupon_id}' ";
	  if($db->query($sql))
	  {
		 alert('优惠卷修改成功...',$filename);
		 exit();
	  }
	  else
	  {
		 alert('优惠卷修改失败');
		 exit();
	  }
}
else if ($act=='del')
{
	 $coupon_id=isset ( $_GET['coupon_id'] ) ? intval ( $_GET['coupon_id'] ) : 0 ;
	 if($coupon_id<1)
	 {
		alert('操作非法');
		exit();
	 }
	 $query=$db->fetch_one("SELECT * FROM `info_q_coupon` WHERE `coupon_id`='{$coupon_id}'");
	 if(!$query)
	 {
		  alert('对不起,数据不存在');
		  exit();
	 }
//	 $file_dir=ROOT.'upload/coupon/'.substr($query['photo'],0,2);  //../upload/coupon/fv
//	 if(is_dir($file_dir))
//	 {
//		delDirAndFile($file_dir);  //删除目录对应
//	 }
	 $sql="DELETE FROM `info_q_coupon` WHERE `coupon_id`='{$coupon_id}'";
	 if($db->query($sql))
	 {
		 alert('优惠卷删除成功',$filename);
		 exit();
	 }
	 else
	 {
		 alert('优惠卷删除失败');
		 exit();
	 }
}else if ($act=='ajax')
{
	$coupon_id=isset ( $_POST['coupon_id'] ) ? trim ( $_POST['coupon_id'] ) : 0 ;
	if($coupon_id<1)
	{
		alert('操作非法');
		exit;
	}
	$sql="SELECT * FROM `info_q_coupon` WHERE `coupon_id`='{$coupon_id}' ";
	$query=$db->fetch_one($sql);
	if(!$query)
	{
		echo 2;
		exit;
	}
	echo $a=$query['enable'] ? 0 : 1;
	$sql="UPDATE `info_q_coupon` SET `enable`='{$a}' WHERE `coupon_id`='{$coupon_id}' ";
	$db->query($sql);

}
else
{

	 $sql="SELECT * FROM `info_q_coupon`";
     $smarty->assign('pl', $pl=licuicui_page($db->num_rows($db->query($sql)),$filename,9));
	 $smarty->assign('coupon_list',$a=$db->fetch_all("SELECT c.*,s.`sort_name` FROM `info_q_coupon` AS c LEFT JOIN `info_q_usual_sort` AS s ON c.`sort_id`=s.`sort_id`  LIMIT {$pl['start']},{$pl['pagesize']}"));
	 $smarty->assign('web',array('title'=>'优惠卷列表'));
	$smarty->display(ADMIN_TPL.'coupon_list.dwt');
}
?>